﻿#region Copyright (c) 2013-08, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	/// <summary>
	/// InvalidOperationException -> app crash when completing ConcurrentExclusiveSchedulerPair
	/// </summary>
	[TestFixture]
	public sealed class Test_Bug13
	{
		private class TestMessage1 : Message
		{
		}
		private class TestMessage2 : Message
		{
		}
		private class TestMessage3 : Message
		{
		}

		private class TestAgent : Agent
		{
			protected TestAgent(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
			}

			[MessageHandler, ConcurrentExecution, ModerateParallelism]
			private void _HandleTestMessage1(TestMessage1 message)
			{
				Int32 sum = 0;
				for (Int32 i = 0; i < 10000; i++)
				{
					sum += i;
				}
			}

			[MessageHandler, IndependentExecution, ModerateParallelism]
			private void _HandleTestMessage2(TestMessage2 message)
			{
				Int32 sum = 0;
				for (Int32 i = 0; i < 10000; i++)
				{
					sum += i;
				}
			}

			[MessageHandler, ExclusiveExecution, ModerateParallelism]
			private void _HandleTestMessage3(TestMessage3 message)
			{
				Int32 sum = 0;
				for (Int32 i = 0; i < 10000; i++)
				{
					sum += i;
				}
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_Single()
		{
			Int32 called = 0;
			Exception exception = null;

			EventHandler<UnobservedTaskExceptionEventArgs> handler = (sender, e) =>
				{
					exception = e.Exception.GetBaseException();
					called++;

					e.SetObserved();
				};

			try
			{
				TaskScheduler.UnobservedTaskException += handler;

				Assert.That(called, Is.EqualTo(0));
				Assert.That(exception, Is.Null);

				var agent = Agent.Factory.SpawnAgent<TestAgent>(
					new AgentEnvironment(),
					new AgentOptions());

				for (Int32 i = 0; i < 10000; i++)
				{
					agent.Post(new TestMessage1());
					agent.Post(new TestMessage2());
					agent.Post(new TestMessage3());
				}

				agent.Dispose();

				Helper.WaitForCondition(5000, 100, () => called >= 0);

				Assert.That(called, Is.EqualTo(0));
				Assert.That(exception, Is.Null);
			}
			finally
			{
				TaskScheduler.UnobservedTaskException -= handler;
			}
		}

		[Test, Ignore]
		public void Test_Call_Often()
		{
			for (Int32 i = 0; i < 100; i++)
				Test_Single();
		}

	}
}

#endif
